package com.sphd.emanu.fragment.details.detailca;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.bank.AccountDetailsBean;
import com.sphd.emanu.pojo.bank.DetailsInfo;
import com.wzutils.TimeUtils;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class DetailCAFragment extends MVPBaseFragment<DetailCAContract.View, DetailCAPresenter> implements DetailCAContract.View {

    @BindView(R.id.shou_text)
    TextView shouText;
    @BindView(R.id.income_bank_name)
    TextView incomeBankName;
    @BindView(R.id.income_bank_num)
    TextView incomeBankNum;
    @BindView(R.id.out_in_image)
    ImageView outInImage;
    @BindView(R.id.fu_text)
    TextView fuText;
    @BindView(R.id.check_bank_name)
    TextView checkBankName;
    @BindView(R.id.zhi_hui_check)
    TextView zhiHuiCheck;
    @BindView(R.id.check_company_name)
    TextView checkCompanyName;
    @BindView(R.id.check_num_hint)
    TextView checkNumHint;
    @BindView(R.id.check_num)
    TextView checkNum;
    @BindView(R.id.check_end_date)
    TextView checkEndDate;
    Unbinder unbinder;
    Bundle bundle;
    private AccountDetailsBean accountDetailsBean;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_detail_ca;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        accountDetailsBean = (AccountDetailsBean) bundle.getSerializable(Constant.ACCOUNT_DETAILS_BEAN);

    }

    @Override
    protected void initData() {
        showProgressDialog();
        mPresenter.getDetail(accountDetailsBean.getId());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setBean(DetailsInfo detailsInfo) {
        dissProgressDialog();
        shouText.setText(TimeUtils.getDayAndTime(Calendar.getInstance(),
                accountDetailsBean.getMethod().equals("3") ? detailsInfo.getContent().getReceiveDate() :
                        detailsInfo.getContent().getReceiveAccountDate())[0]);
        fuText.setText(TimeUtils.getDayAndTime(Calendar.getInstance(),
                detailsInfo.getContent().getReceiveDate())[0]);
        zhiHuiCheck.setText(accountDetailsBean.getMethod().equals("3") ? "支票" : "汇票");
        checkNumHint.setText(accountDetailsBean.getMethod().equals("3") ? "支票号" : "汇票号");
        incomeBankName.setText(splitNotNumber(accountDetailsBean.getAccountBank()));
        incomeBankNum.setText(getNumbers(accountDetailsBean.getAccountBank()));
        checkCompanyName.setText((
                accountDetailsBean.getMethod().equals("3") ? "出具支票单位" : "出具汇票单位") +
                detailsInfo.getContent().getOriginalCorp());
        checkBankName.setText(splitNotNumber(accountDetailsBean.getAccountBank()));
        checkNum.setText(detailsInfo.getContent().getReturnNo());
        checkEndDate.setText(TimeUtils.getDayAndTime(Calendar.getInstance(),
                detailsInfo.getContent().getExpireDate())[0]);
    }

    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    // 截取非数字
    public String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

}
